package wangshuai.risk.action;

import fanjingzhu.check.service.ApplicationService;
import fanjingzhu.check.service.EnterpriseService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import com.opensymphony.xwork2.ActionSupport;
import publics.model.Ratio;
import publics.util.MyUtils;
import publics.util.Page;
import wangshuai.risk.service.RatioService;



public class RatioAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	
	private RatioService ratioService;  //spring注入进来的
	private ApplicationService applicationService;
	
	public RatioService getRatioService() {
		return ratioService;
	}
	@Resource(name="ratioService")
	public void setRatioService(RatioService ratioService) {
		this.ratioService = ratioService;
		System.out.println("ratioServiceImpl inject in RatioAcion!");
	}
	
	public ApplicationService getApplicationService() {
		return applicationService;
	}
	@Resource(name="applicationService")
	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
		System.out.println("applicationServiceImpl inject in RatioAcion!");
	}




	private boolean success;   //此参数为必须的， extjs根据这个参数的值判断是否成功执行操作
	private Page page;   //这是分页查询用的辅助类，用此类可以解决分页查询中要返回多个参数的问题，推荐使用
	private String conditions;//查询条件（即搜索页面上文本框中输入的值），多个条件用空格分开
	private int start;   //分页查询的 要查询数据的开始号 ，和offset值相同
	private int limit;  // 每页多少个记录

	private Ratio ratio;

	private Integer ratioId;
	private Integer applicationId;


	/**
	 * 添加分公司
	 * @return
	 */
	public String saveRatio() {
		System.out.println("已经进入 Action 的 saveRatio方法！");//测试用，可去掉
//		this.setEnterpriseId(1);//测试用
		this.getRatio().setApplication(this.getApplicationService().findById(applicationId)); 
		this.getRatio().setChooseTime(true);//测试用 请之后修改好
		System.out.println("ratio表数据:"+ratio.toString());
		ratioId = (Integer) this.getRatioService().savaRatio(ratio);//保存比率分析表
		
		if (ratioId != null) {
			success = true;
		}else{
			success = false;  //失败返回
		}
		System.out.println("准备返回，success="+success);
		return SUCCESS;
	}
	


	/**
	 * 根据条件分页查找公司
	 * @return
	 */
	public String findAllRatio() {
		
		List<String> conditionList = new ArrayList<String>();   //模糊查询使用
		MyUtils.addToCollection(conditionList, MyUtils.split(conditions, " ,，"));//模糊查询使用
		page = new Page(); // 实例化分页对象
		page.setConditions(conditionList);// 设置模糊查询条件
		/*for(Iterator i = conditionList.iterator(); i.hasNext();){//测试用
			System.out.println(i.next()+", ");
		}*/
		page.setStart(start);
		page.setLimit(limit = (limit == 0) ? 20 : limit);
		
		page = this.getRatioService().findByPage(page);  //查询 
		if ((null != page.getRoot() && page.getTotalProperty() > 0 )||(null == page.getRoot() && page.getTotalProperty() == 0 )) {
			page.setSuccess(true);
//			System.out.println("成功返回数据，准备返回。page.root="+page.getRoot());//测试用
		}else{
			page.setSuccess(false);//查找失败
		}
		return SUCCESS;
	}

	/**
	 * 删除财务比率表
	 * 
	 * @return
	 */
	public String deleteRatio() {
		
		if(Integer.valueOf(ratioId) == null || Integer.valueOf(ratioId) < 1){
			System.out.println("修改失败！ 比率表id为="+ratioId);
			success = false;
		}else{
			success = this.getRatioService().deleteById(Integer.valueOf(ratioId));
		}
		return SUCCESS;
	}
	
	/**
	 * 修改财务比率指定字段的值
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateRatio() throws Exception {
		
		if(Integer.valueOf(this.getRatio().getId()) < 1){
			System.out.println("修改失败！ 比率表id为="+this.getRatio().getId());
			success = false;
		}else{
			this.getRatio().setApplication(this.getApplicationService().findById(Integer.valueOf(this.getApplicationId()))); 
			//把外键字段（申请表） set进去，（此字段为Object，所有要这么做，请认真体会）
			
			success = this.getRatioService().updateRatio(ratio);
		}

		return SUCCESS;
	}
	
	
	/*	*//**
	 * 查找所有财务比率表
	 * 
	 * @return
	 *//*
	public String findAll(){
		page = new Page();
		page.setRoot(companyService.findAll());
		return SUCCESS;
	}*/
	
	

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getConditions() {
		return conditions;
	}

	public void setConditions(String conditions) {
		this.conditions = conditions;
	}
	/**
	 * @return the ratio
	 */
	public Ratio getRatio() {
		return ratio;
	}
	/**
	 * @return the ratioId
	 */
	public Integer getRatioId() {
		return ratioId;
	}
	/**
	 * @param ratio the ratio to set
	 */
	public void setRatio(Ratio ratio) {
		this.ratio = ratio;
	}
	/**
	 * @param ratioId the ratioId to set
	 */
	public void setRatioId(Integer ratioId) {
		this.ratioId = ratioId;
	}
	/**
	 * @return the applicationId
	 */
	public Integer getApplicationId() {
		return applicationId;
	}
	/**
	 * @param applicationId the applicationId to set
	 */
	public void setApplicationId(Integer applicationId) {
		this.applicationId = applicationId;
	}



}
